package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.EnrollInfo;
import com.xmy.cultivate.entity.EnrollTeam;
import com.xmy.cultivate.entity.EnrollTeamMember;
import com.xmy.cultivate.mapper.EnrollInfoMapper;
import com.xmy.cultivate.mapper.EnrollTeamMemberMapper;
import com.xmy.cultivate.service.IEnrollTeamMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 招生战队成员 服务实现类
 * </p>
 *
 * @author min
 * @since 2023-02-10
 */
@Service
public class EnrollTeamMemberServiceImpl extends ServiceImpl<EnrollTeamMemberMapper, EnrollTeamMember> implements IEnrollTeamMemberService {

    @Autowired
    EnrollTeamMemberMapper enrollTeamMemberMapper;

    @Autowired
    EnrollInfoMapper enrollInfoMapper;



    public Long getTeamIdForteacherId(Long teacherId){

        QueryWrapper<EnrollTeamMember> enrollTeamMemberQueryWrapper = new QueryWrapper<>();
        enrollTeamMemberQueryWrapper.eq("teacher_id",teacherId);
        enrollTeamMemberQueryWrapper.eq("deleted",0);
        enrollTeamMemberQueryWrapper.eq("status",1);
        enrollTeamMemberQueryWrapper.last("limit 1");

        Long teamId = 0L;

        EnrollTeamMember enrollTeamMember = enrollTeamMemberMapper.selectOne(enrollTeamMemberQueryWrapper);
        if(enrollTeamMember !=null){
            teamId = enrollTeamMember.getTeamId();
        }

        return teamId;
    }

    @Transactional
    public boolean switchTeam(EnrollTeamMember enrollTeamMember){

        QueryWrapper<EnrollTeamMember> enrollTeamMemberQueryWrapper = new QueryWrapper<>();

        //System.out.println("enrollTeamMember:"+enrollTeamMember.toString());
        enrollTeamMemberQueryWrapper.eq("teacher_id",enrollTeamMember.getTeacherId());
        enrollTeamMemberQueryWrapper.eq("status",1);
        enrollTeamMemberQueryWrapper.eq("deleted",0);
        enrollTeamMemberQueryWrapper.last("limit 1");


        EnrollTeamMember enrollTeamMemberInfo = enrollTeamMemberMapper.selectOne(enrollTeamMemberQueryWrapper);
        //System.out.println("enrollTeamMemberInfo:"+enrollTeamMemberInfo.toString());

        boolean rs = false;

        if(enrollTeamMemberInfo != null){
             if (enrollTeamMember.getEndDate()!=null){
                 QueryWrapper<EnrollInfo> enrollInfoQueryWrapper = new QueryWrapper<>();
                 enrollInfoQueryWrapper.eq("teacher_id",enrollTeamMember.getTeacherId());
                 enrollInfoQueryWrapper.eq("deleted",0);
                 enrollInfoQueryWrapper.gt("add_date",enrollTeamMember.getEndDate());
                 List<EnrollInfo> enrollInfoList = enrollInfoMapper.selectList(enrollInfoQueryWrapper);
                 for (EnrollInfo enrollInfo :enrollInfoList){
                     enrollInfo.setTeamId(enrollTeamMember.getTeamId());
                     enrollInfoMapper.updateById(enrollInfo);
                 }
                 //设置原来战队结束
                 enrollTeamMemberInfo.setEndDate(enrollTeamMember.getEndDate());
                 enrollTeamMemberInfo.setStatus(0);
                 enrollTeamMemberMapper.updateById(enrollTeamMemberInfo);
                //添加到新的战队
                 enrollTeamMemberQueryWrapper = new QueryWrapper<>();
                 enrollTeamMemberQueryWrapper.eq("teacher_id",enrollTeamMember.getTeacherId());
                 enrollTeamMemberQueryWrapper.eq("status",1);
                 enrollTeamMemberQueryWrapper.eq("deleted",0);
                 Long count = enrollTeamMemberMapper.selectCount(enrollTeamMemberQueryWrapper);
                 if (count.equals(0L)){
                      EnrollTeamMember enrollTeamMemberAddInfo = new EnrollTeamMember();
                     enrollTeamMemberAddInfo.setTeamId(enrollTeamMember.getTeamId());
                     enrollTeamMemberAddInfo.setTeacherId(enrollTeamMember.getTeacherId());
                     enrollTeamMemberAddInfo.setStartDate(enrollTeamMember.getEndDate());
                     enrollTeamMemberAddInfo.setStatus(1);
                     enrollTeamMemberMapper.insert(enrollTeamMemberAddInfo);
                 }
                 rs = true;
             }
        }
        return rs;
    }
}
